Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Reactjs Interview Questions
Description: React is a JavaScript-based UI development library. Facebook and an open-source developer community run it. Although React is a library rather than a language, it is widely used in web development. The library first appeared in May 2013 and is now one of the most commonly used frontend libraries for web development.in this notes i have done 300+ interview questions and answers so that is best for interview from my side
Description: React is a JavaScript-based UI development library. Facebook and an open-source developer community run it. Although React is a library rather than a language, it is widely used in web development. The library first appeared in May 2013 and is now one of the most commonly used frontend libraries for web development.in this notes i have done 300+ interview questions and answers so that is best for interview from my side
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
React Interview Questions & Answers
Click :star:if you like the project
...
Follow me [@SudheerJonna](https://twitter
...
Note: This repository is specific to ReactJS
...
Downloading PDF/Epub formats
You can download the PDF and Epub version of this repository from the latest
run on the actions tab
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Questions
Core React
What is React?
What are the major features of React?
What is JSX?
What is the difference between Element and
Component?
How to create components in React?
When to use a Class Component over a Function
Component?
What are Pure Components?
What is state in React?
What are props in React?
What is the difference between state and props?
Why should we not update the state directly?
What is the purpose of callback function as an
argument of setState()?
What is the difference between HTML and React event
handling?
How to bind methods or event handlers in JSX
callbacks?
How to pass a parameter to an event handler or
callback?
What are synthetic events in React?
What are inline conditional expressions?
What is “key” prop and what is the benefit of using it
in arrays of elements?
What is the use of refs?
How to create refs?
What are forward refs?
1
No
...
Questions
60
61
62
63
64
65
How to use InnerHtml in React?
How to use styles in React?
How events are different in React?
What will happen if you use setState in constructor?
What is the impact of indexes as keys?
Is it good to use setState() in componentWillMount()
method?
What will happen if you use props in initial state?
How do you conditionally render components?
Why we need to be careful when spreading props on
DOM elements??
How you use decorators in React?
How do you memoize a component?
How you implement Server-Side Rendering or SSR?
How to enable production mode in React?
What is CRA and its benefits?
What is the lifecycle methods order in mounting?
What are the lifecycle methods going to be deprecated
in React v16?
What is the purpose of getDerivedStateFromProps()
lifecycle method?
What is the purpose of getSnapshotBeforeUpdate()
lifecycle method?
Do Hooks replace render props and higher order
components?
What is the recommended way for naming components?
What is the recommended ordering of methods in
component class?
What is a switching component?
Why we need to pass a function to setState()?
What is strict mode in React?
What are React Mixins?
Why is isMounted() an anti-pattern and what is the
proper solution?
What are the Pointer Events supported in React?
Why should component names start with capital letter?
Are custom DOM attributes supported in React v16?
What is the difference between constructor and
getInitialState?
Can you force a component to re-render without calling
setState?
What is the difference between super() and
super(props) in React using ES6 classes?
How to loop inside JSX?
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
3
No
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Questions
React Router
What is React Router?
How React Router is different from history library?
What are the
v4?
What is the purpose of push and replace methods of
history?
How do you programmatically navigate using React
router v4?
How to get query parameters in React Router v4
Why you get “Router may have only one child element”
warning?
How to pass params to history
...
Questions
162
163
164
How to use connect from React Redux?
How to reset state in Redux?
Whats the purpose of at symbol in the redux connect
decorator?
What is the difference between React context and
React Redux?
Why are Redux state functions called reducers?
How to make AJAX request in Redux?
Should I keep all component’s state in Redux store?
What is the proper way to access Redux store?
What is the difference between component and
container in React Redux?
What is the purpose of the constants in Redux?
What are the different ways to write
mapDispatchToProps()?
What is the use of the ownProps parameter in
mapStateToProps() and mapDispatchToProps()?
How to structure Redux top level directories?
What is redux-saga?
What is the mental model of redux-saga?
What are the differences between call and put in
redux-saga
What is Redux Thunk?
What are the differences between redux-saga and
redux-thunk
What is Redux DevTools?
What are the features of Redux DevTools?
What are Redux selectors and Why to use them?
What is Redux Form?
What are the main features of Redux Form?
How to add multiple middlewares to Redux?
How to set initial state in Redux?
How Relay is different from Redux?
What is an action in Redux?
React Native
What is the difference between React Native and
React?
How to test React Native apps?
How to do logging in React Native?
How to debug your React Native?
React supported libraries and Integration
What is reselect and how it works?
What is Flow?
What is the difference between Flow and PropTypes?
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
188
189
190
191
192
193
194
6
No
...
js?
What is the difference between React and Angular?
Why React tab is not showing up in DevTools?
What are styled components?
Give an example of Styled Components?
What is Relay?
How to use TypeScript in create-react-app application?
Miscellaneous
What are the main features of reselect library?
Give an example of reselect usage?
Does the statics object work with ES6 classes in React?
Can Redux only be used with React?
Do you need to have a particular build tool to use
Redux?
How Redux Form initialValues get updated from state?
How React PropTypes allow different type for one
prop?
Can I import an SVG file as react component?
Why are inline ref callbacks or functions not
recommended?
What is render hijacking in React?
What are HOC factory implementations?
How to pass numbers to React component?
Do I need to keep all my state into Redux? Should I
ever use react internal state?
What is the purpose of registerServiceWorker in React?
What is React memo function?
What is React lazy function?
How to prevent unnecessary updates using setState?
How do you render Array, Strings and Numbers in
React 16 Version?
How to use class field declarations syntax in React
classes?
What are hooks?
What rules need to be followed for hooks?
How to ensure hooks followed the rules in your project?
What are the differences between Flux and Redux?
What are the benefits of React Router V4?
Can you describe about componentDidCatch lifecycle
method signature?
206
207
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
7
No
...
Questions
267
What are the conditions to safely use the index as a
key?
Is it keys should be globally unique?
What is the popular choice for form handling?
What are the advantages of formik over redux form
library?
Why do you not required to use inheritance?
Can I use web components in react application?
What is dynamic import?
What are loadable components?
What is suspense component?
What is route based code splitting?
Give an example on How to use context?
What is the purpose of default value in context?
How do you use contextType?
What is a consumer?
How do you solve performance corner cases while using
context?
What is the purpose of forward ref in HOCs?
Is it ref argument available for all functions or class
components?
Why do you need additional care for component
libraries while using forward refs?
How to create react class components without ES6?
Is it possible to use react without JSX?
What is diffing algorithm?
What are the rules covered by diffing algorithm?
When do you need to use refs?
Is it prop must be named as render for render props?
What are the problems of using render props with pure
components?
How do you create HOC using render props?
What is windowing technique?
How do you print falsy values in JSX?
What is the typical use case of portals?
How do you set default value for uncontrolled
component?
What is your favorite React stack?
What is the difference between Real DOM and Virtual
DOM?
How to add Bootstrap to a react application?
Can you list down top websites or applications using
react as front end framework?
Is it recommended to use CSS In JS technique in React?
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
9
No
...
9?
What is the purpose of eslint plugin for hooks?
What is the difference between Imperative and
Declarative in React?
What are the benefits of using typescript with reactjs?
How do you make sure that user remains authenticated
on page refresh while using Context API State
Management?
What are the benefits of new JSX transform?
How does new JSX transform different from old
transform?
How do you get redux scaffolding using
create-react-app?
What are React Server components?
What is prop drilling?
What is state mutation and how to prevent it?
What is the difference between useState and useRef
hook?
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
10
Core React
1
...
It is used
for handling view layer for web and mobile apps
...
React was first
deployed on Facebook’s News Feed in 2011 and on Instagram in 2012
...
What are the major features of React?
The major features of React are:
• It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive
...
• Follows Unidirectional data flow or data binding
...
� Back to Top
3
...
Basically it just provides syntactic sugar for the
React
...
In the example below text inside
tag is returned as JavaScript function to the render function
...
Component {
render()
{
return(
{'Welcome to React
world!'}
)
} }
� Back to Top
4
...
Elements can
11
contain other Elements in their props
...
Once an element is created, it is never mutated
...
createElement(
'div',
{id: 'login-btn'},
'Login'
)
The above React
...
render():
Login
Whereas a component can be declared in several different ways
...
In either case, it takes props as an input, and returns a JSX tree as the
output:
const Button = ({ onLogin }) =>
Login
Then JSX gets transpiled to a React
...
createElement(
'div',
{ id: 'login-btn', onClick: onLogin },
'Login'
)
� Back to Top
5
...
1
...
Those are pure JavaScript functions that accept props object
as the first parameter and return React elements:
12
“jsx harmony function Greeting({ message }) {
{Hello, ${message}‘}
return
} “‘
2
...
The above function component can be written as:
jsx harmony
render() {
} }
class Greeting extends React
...
props
...
When to use a Class Component over a Function Component?
If the component needs state or lifecycle methods then use class component otherwise use function component
...
8 with
the addition of Hooks, you could use state , lifecycle methods and other
features that were only available in class component right in your function
component
...
If the React
element was previously rendered into container, it will perform an update
on it and only mutate the DOM as necessary to reflect the latest changes
...
render(element, container, [callback])
If the optional callback is provided, it will be executed after the component
is rendered or updated
...
What is ReactDOMServer?
The ReactDOMServer object enables you to render components to static
markup (typically used on node server)
...
The following methods can be used in both
the server and browser environments:
1
...
renderToStaticMarkup()
For example, you generally run a Node-based web server like Express,
Hapi, or Koa, and you call renderToString to render your root component
to a string, which you then send as response
...
/MyPage'
app
...
write('My Page ')
res
...
write(renderToString( ))
res
...
end()
})
� Back to Top
60
...
Just like innerHTML, it is risky to use
this attribute considering cross-site scripting (XSS) attacks
...
In this example MyComponent uses dangerouslySetInnerHTML attribute
for setting HTML markup:
“‘jsx harmony function createMarkup() { return { __html: ‘First · Second’ } }
function MyComponent() { return
} “‘
� Back to Top
61
...
This is consistent with the DOM style
JavaScript property, is more efficient, and prevents XSS security holes
...
g
...
style
...
� Back to Top
62
...
React event handlers are named using camelCase, rather than lowercase
...
With JSX you pass a function as the event handler, rather than a
string
...
What will happen if you use setState() in constructor?
When you use setState(), then apart from assigning to the object state
React also re-renders the component and all its children
...
So
we need to use this
...
� Back to Top
64
...
In the below code snippet each element’s key will be based on ordering,
rather than tied to the data that is being represented
...
jsx harmony {todos
...
todo}
If you use element data for unique key, assuming todo
...
jsx harmony {todos
...
todo}
key={todo
...
Is it good to use setState() in componentWillMount() method?
Yes, it is safe to use setState() inside componentWillMount() method
...
componentWillMount() is
invoked immediately before mounting occurs
...
Avoid
introducing any side-effects or subscriptions in this method
...
jsx harmony componentDidMount() {
axios
...
then((result) => {
this
...
result
...
What will happen if you use props in initial state?
If the props on the component are changed without the component being
refreshed, the new prop value will never be displayed because the constructor function will never update the current state of the component
...
The below component won’t display the updated input value:
“‘jsx harmony class MyComponent extends React
...
state = {
records: [],
inputValue: this
...
inputValue
};
}
render() { return
41
{this
...
inputValue}
}}
Using props inside render method will update the value:
```jsx harmony
class MyComponent extends React
...
state = {
record: []
}
}
render() {
return {this
...
inputValue}
}
}
� Back to Top
67
...
JSX does not render false or undefined, so you can use conditional
short-circuiting to render a given part of your component only if a certain
condition is true
...
jsx harmony const MyComponent = ({ name, address }) => (
{name}
{address
? {address}
: {'Address is not available'}
}
)
� Back to Top
68
...
Instead we can use prop destructuring
42
with
...
For example,
“‘jsx harmony const ComponentA = () =>
const ComponentB = ({ isDisplay, …domProps }) => {‘ComponentB’}
“‘
� Back to Top
69
...
Decorators are flexible and readable way of
modifying component functionality
...
Component
{ //…
...
Component { componentDidMount() { document
...
this
...
� Back to Top
70
...
For example moize library can memoize the component in another component
...
/components/Component’ // this module exports a non-memoized component
43
const MemoizedFoo = moize
...
6
...
memo`
...
memo(function MemoComponent(props) {
/* render using props */
});
OR
export default React
...
How you implement Server Side Rendering or SSR?
React is already equipped to handle rendering on Node servers
...
“‘jsx harmony import ReactDOMServer from ‘react-dom/server’ import
App from ‘
...
renderToString() “‘
This method will output the regular HTML as a string, which can be then
placed inside a page body as part of the server response
...
� Back to Top
72
...
Apart from this, if you minify the code, for example,
Uglify’s dead-code elimination to strip out development only code and
comments, it will drastically reduce the size of your bundle
...
44
What is CRA and its benefits?
The create-react-app CLI tool allows you to quickly create & run React
applications with no configuration step
...
2
...
4
...
Language extras beyond ES6 like the object spread operator
...
A fast interactive unit test runner with built-in support for coverage
reporting
...
A live development server that warns about common mistakes
...
A build script to bundle JS, CSS, and images for production, with
hashes and sourcemaps
...
What is the lifecycle methods order in mounting?
The lifecycle methods are called in the following order when an instance
of a component is being created and inserted into the DOM
...
2
...
4
...
45
What are the lifecycle methods going to be deprecated in React
v16?
The following lifecycle methods going to be unsafe coding practices and
will be more problematic with async rendering
...
componentWillMount()
2
...
componentWillUpdate()
Starting with React v16
...
� Back to Top
76
...
It can
return an object to update state, or null to indicate that the new props
do not require any state updates
...
Component {
static getDerivedStateFromProps(props, state) {
//
...
� Back to Top
77
...
The return value from this method will be passed
as the third parameter to componentDidUpdate()
...
Component {
getSnapshotBeforeUpdate(prevProps, prevState) {
//
...
� Back to Top
78
...
� Back to Top
79
...
Using displayName for naming component:
export default React
...
})
The recommended approach:
export default class TodoApp extends React
...
}
also
const TodoApp = () => {
//
...
What is the recommended ordering of methods in component
class?
Recommended ordering of methods from mounting to render stage:
1
...
constructor()
47
3
...
5
...
7
...
9
...
11
...
getter methods for render like getSelectReason() or getFooterContent()
13
...
render()
� Back to Top
81
...
We need to use object to map prop values to components
...
/HomePage’ import AboutPage
from ‘
...
/ServicesPage’ import ContactPage from ‘
...
page] || ContactPage
return }
// The keys of the PAGES object can be used in the prop types to catch
dev-time errors
...
propTypes = { page: PropTypes
...
keys(PAGES))
...
Why we need to pass a function to setState()?
The reason behind for this is that setState() is an asynchronous operation
...
That means you
48
should not rely on the current state when calling setState() since you
can’t be sure what that state will be
...
By doing this you
can avoid issues with the user getting the old state value on access due to
the asynchronous nature of setState()
...
After three consecutive increment
operations, the value is going to be incremented only by one
...
state
...
setState({ count: this
...
count + 1 })
this
...
state
...
setState({ count: this
...
count + 1 })
// this
...
count === 1, not 3
If we pass a function to setState(), the count gets incremented correctly
...
setState((prevState, props) => ({
count: prevState
...
increment
}))
// this
...
count === 3 as expected
(OR)
Why function is preferred over object for setState()?
React may batch multiple setState() calls into a single update for performance
...
props and this
...
This counter example will fail to update as expected:
// Wrong
this
...
state
...
props
...
That function will receive the previous state as the first argument,
and the props at the time the update is applied as the second argument
...
setState((prevState, props) => ({
counter: prevState
...
increment
}))
� Back to Top
83
...
StrictMode is a useful component for highlighting potential problems in an application
...
It activates additional checks and warnings for its descendants
...
“‘jsx harmony import React from ‘react’
function ExampleApplication() { return (
...
StrictMode>
) } “‘
In the example above, the strict mode checks apply to
and components only
...
What are React Mixins?
Mixins are a way to totally separate components to have a common functionality
...
One of the most commonly used mixins is PureRenderMixin
...
createClass({
mixins: [PureRenderMixin],
//
...
50
Why is isMounted() an anti-pattern and what is the proper solution?
The primary use case for isMounted() is to avoid calling setState() after
a component has been unmounted, because it will emit a warning
...
isMounted()) {
this
...
})
}
Checking isMounted() before calling setState() does eliminate the warning, but it also defeats the purpose of the warning
...
An optimal solution would be to find places where setState() might be
called after a component has unmounted, and fix them
...
Ideally, any callbacks should be canceled in componentWillUnmount(), prior to unmounting
...
What are the Pointer Events supported in React?
Pointer Events provide a unified way of handling all input events
...
We need to remember that
these events will only work in browsers that support the Pointer Events
specification
...
2
...
4
...
6
...
8
...
10
...
51
Why should component names start with capital letter?
If you are rendering your component using JSX, the name of that
component has to begin with a capital letter otherwise React will throw
an error as an unrecognized tag
...
jsx harmony
class SomeComponent extends Component { // Code goes here }
You can define component class which name starts with lowercase letter,
but when it’s imported it should have capital letter
...
/MyComponent'
What are the exceptions on React component naming? The component names should start with an uppercase letter but there are few exceptions to this convention
...
For example, the
below tag can be compiled to a valid component,
jsx harmony
render() {
return (
// `React
...
component)`
...
Are custom DOM attributes supported in React v16?
Yes
...
If you
wrote JSX with an attribute that React doesn’t recognize, React would
just skip it
...
� Back to Top
89
...
createClass()
...
This way it can conditionally render component
...
loggedIn) {
return null;
}
return (
welcome, {props
...
Component {
constructor(props) {
super(props);
this
...
state
...
state
...
� Back to Top
267
...
1
...
The items in the list have no ids
3
...
� Back to Top
268
...
i
...
For example, the below Book component uses two arrays with different
arrays,
function Book(props) {
const index = (
{props
...
map((page) =>
-
...
title}
)}
);
const content = props
...
map((page) =>
...
title}
{page
...
pageNumber}
);
return (
{index}
{content}
);
}
� Back to Top
269
...
In detail, You can categorize them as follows,
1
...
Validation and error messages
3
...
� Back to Top
270
...
The form state is inherently short-term and local, so tracking it in
Redux (or any kind of Flux library) is unnecessary
...
Redux-Form calls your entire top-level Redux reducer multiple times
ON EVERY SINGLE KEYSTROKE
...
3
...
5 kB minified gzipped whereas Formik is 12
...
Why are you not required to use inheritance?
In React, it is recommended to use composition over inheritance to reuse
code between components
...
Whereas, If you want to reuse non-UI functionality between components, it is suggested to extract it into a separate JavaScript
module
...
� Back to Top
272
...
Even though
many developers won’t use this combination, it may require especially
if you are using third-party UI components that are written using Web
Components
...
/App
...
129
What is dynamic import?
You can achieve code-splitting in your app using dynamic import
...
Normal Import
import { add } from '
...
log(add(10, 20));
2
...
/math")
...
log(math
...
What are loadable components?
If you want to do code-splitting in a server rendered app, it is recommend
to use Loadable Components because React
...
Loadable lets you render a dynamic
import as a regular component
...
/OtherComponent'))
function MyComponent() {
return (
)
}
Now OtherComponent will be loaded in a separated bundle
� Back to Top
275
...
This can be
done using Suspense component
...
lazy(() => import('
...
}>
{'Welcome to React
world!'}
return
} “‘
2
...
The above function component can be written as:
jsx harmony
render() {
} }
class Greeting extends React
...
props
...
When to use a Class Component over a Function Component?
If the component needs state or lifecycle methods then use class component otherwise use function component
...
8 with
the addition of Hooks, you could use state , lifecycle methods and other
features that were only available in class component right in your function
component
...
If the React
element was previously rendered into container, it will perform an update
on it and only mutate the DOM as necessary to reflect the latest changes
...
render(element, container, [callback])
If the optional callback is provided, it will be executed after the component
is rendered or updated
...
What is ReactDOMServer?
The ReactDOMServer object enables you to render components to static
markup (typically used on node server)
...
The following methods can be used in both
the server and browser environments:
1
...
renderToStaticMarkup()
For example, you generally run a Node-based web server like Express,
Hapi, or Koa, and you call renderToString to render your root component
to a string, which you then send as response
...
/MyPage'
app
...
write('
res
...
write(renderToString(
res
...
end()
})
� Back to Top
60
...
Just like innerHTML, it is risky to use
this attribute considering cross-site scripting (XSS) attacks
...
In this example MyComponent uses dangerouslySetInnerHTML attribute
for setting HTML markup:
“‘jsx harmony function createMarkup() { return { __html: ‘First · Second’ } }
function MyComponent() { return
} “‘
� Back to Top
61
...
This is consistent with the DOM style
JavaScript property, is more efficient, and prevents XSS security holes
...
g
...
style
...
� Back to Top
62
...
React event handlers are named using camelCase, rather than lowercase
...
With JSX you pass a function as the event handler, rather than a
string
...
What will happen if you use setState() in constructor?
When you use setState(), then apart from assigning to the object state
React also re-renders the component and all its children
...
So
we need to use this
...
� Back to Top
64
...
In the below code snippet each element’s key will be based on ordering,
rather than tied to the data that is being represented
...
jsx harmony {todos
...
todo}
If you use element data for unique key, assuming todo
...
jsx harmony {todos
...
todo}
key={todo
...
Is it good to use setState() in componentWillMount() method?
Yes, it is safe to use setState() inside componentWillMount() method
...
componentWillMount() is
invoked immediately before mounting occurs
...
Avoid
introducing any side-effects or subscriptions in this method
...
jsx harmony componentDidMount() {
axios
...
then((result) => {
this
...
result
...
What will happen if you use props in initial state?
If the props on the component are changed without the component being
refreshed, the new prop value will never be displayed because the constructor function will never update the current state of the component
...
The below component won’t display the updated input value:
“‘jsx harmony class MyComponent extends React
...
state = {
records: [],
inputValue: this
...
inputValue
};
}
render() { return
41
{this
...
inputValue}
}}
Using props inside render method will update the value:
```jsx harmony
class MyComponent extends React
...
state = {
record: []
}
}
render() {
return
{this
...
inputValue}
...
inputValue}
}
}
� Back to Top
67
...
JSX does not render false or undefined, so you can use conditional
short-circuiting to render a given part of your component only if a certain
condition is true
...
jsx harmony const MyComponent = ({ name, address }) => (
{name}
{address
?
{address}
:
{'Address is not available'}
}
� Back to Top
68
...
Instead we can use prop destructuring
42
with
...
For example,
“‘jsx harmony const ComponentA = () =>
const ComponentB = ({ isDisplay, …domProps }) =>
{‘ComponentB’}
“‘
� Back to Top
69
...
Decorators are flexible and readable way of
modifying component functionality
...
Component
{ //…
...
Component { componentDidMount() { document
...
this
...
� Back to Top
70
...
For example moize library can memoize the component in another component
...
/components/Component’ // this module exports a non-memoized component
43
const MemoizedFoo = moize
...
6
...
memo`
...
memo(function MemoComponent(props) {
/* render using props */
});
OR
export default React
...
How you implement Server Side Rendering or SSR?
React is already equipped to handle rendering on Node servers
...
“‘jsx harmony import ReactDOMServer from ‘react-dom/server’ import
App from ‘
...
renderToString() “‘
This method will output the regular HTML as a string, which can be then
placed inside a page body as part of the server response
...
� Back to Top
72
...
Apart from this, if you minify the code, for example,
Uglify’s dead-code elimination to strip out development only code and
comments, it will drastically reduce the size of your bundle
...
44
What is CRA and its benefits?
The create-react-app CLI tool allows you to quickly create & run React
applications with no configuration step
...
2
...
4
...
Language extras beyond ES6 like the object spread operator
...
A fast interactive unit test runner with built-in support for coverage
reporting
...
A live development server that warns about common mistakes
...
A build script to bundle JS, CSS, and images for production, with
hashes and sourcemaps
...
What is the lifecycle methods order in mounting?
The lifecycle methods are called in the following order when an instance
of a component is being created and inserted into the DOM
...
2
...
4
...
45
What are the lifecycle methods going to be deprecated in React
v16?
The following lifecycle methods going to be unsafe coding practices and
will be more problematic with async rendering
...
componentWillMount()
2
...
componentWillUpdate()
Starting with React v16
...
� Back to Top
76
...
It can
return an object to update state, or null to indicate that the new props
do not require any state updates
...
Component {
static getDerivedStateFromProps(props, state) {
//
...
� Back to Top
77
...
The return value from this method will be passed
as the third parameter to componentDidUpdate()
...
Component {
getSnapshotBeforeUpdate(prevProps, prevState) {
//
...
� Back to Top
78
...
� Back to Top
79
...
Using displayName for naming component:
export default React
...
})
The recommended approach:
export default class TodoApp extends React
...
}
also
const TodoApp = () => {
//
...
What is the recommended ordering of methods in component
class?
Recommended ordering of methods from mounting to render stage:
1
...
constructor()
47
3
...
5
...
7
...
9
...
11
...
getter methods for render like getSelectReason() or getFooterContent()
13
...
render()
� Back to Top
81
...
We need to use object to map prop values to components
...
/HomePage’ import AboutPage
from ‘
...
/ServicesPage’ import ContactPage from ‘
...
page] || ContactPage
return }
// The keys of the PAGES object can be used in the prop types to catch
dev-time errors
...
propTypes = { page: PropTypes
...
keys(PAGES))
...
Why we need to pass a function to setState()?
The reason behind for this is that setState() is an asynchronous operation
...
That means you
48
should not rely on the current state when calling setState() since you
can’t be sure what that state will be
...
By doing this you
can avoid issues with the user getting the old state value on access due to
the asynchronous nature of setState()
...
After three consecutive increment
operations, the value is going to be incremented only by one
...
state
...
setState({ count: this
...
count + 1 })
this
...
state
...
setState({ count: this
...
count + 1 })
// this
...
count === 1, not 3
If we pass a function to setState(), the count gets incremented correctly
...
setState((prevState, props) => ({
count: prevState
...
increment
}))
// this
...
count === 3 as expected
(OR)
Why function is preferred over object for setState()?
React may batch multiple setState() calls into a single update for performance
...
props and this
...
This counter example will fail to update as expected:
// Wrong
this
...
state
...
props
...
That function will receive the previous state as the first argument,
and the props at the time the update is applied as the second argument
...
setState((prevState, props) => ({
counter: prevState
...
increment
}))
� Back to Top
83
...
StrictMode is a useful component for highlighting potential problems in an application
...
It activates additional checks and warnings for its descendants
...
“‘jsx harmony import React from ‘react’
function ExampleApplication() { return (
...
StrictMode>
“‘
� Back to Top
69
...
Decorators are flexible and readable way of
modifying component functionality
...
Component
{ //…
...
Component { componentDidMount() { document
...
this
...
� Back to Top
70
...
For example moize library can memoize the component in another component
...
/components/Component’ // this module exports a non-memoized component
43
const MemoizedFoo = moize
...
6
...
memo`
...
memo(function MemoComponent(props) {
/* render using props */
});
OR
export default React
...
How you implement Server Side Rendering or SSR?
React is already equipped to handle rendering on Node servers
...
“‘jsx harmony import ReactDOMServer from ‘react-dom/server’ import
App from ‘
...
renderToString() “‘
This method will output the regular HTML as a string, which can be then
placed inside a page body as part of the server response
...
� Back to Top
72
...
Apart from this, if you minify the code, for example,
Uglify’s dead-code elimination to strip out development only code and
comments, it will drastically reduce the size of your bundle
...
44
What is CRA and its benefits?
The create-react-app CLI tool allows you to quickly create & run React
applications with no configuration step
...
2
...
4
...
Language extras beyond ES6 like the object spread operator
...
A fast interactive unit test runner with built-in support for coverage
reporting
...
A live development server that warns about common mistakes
...
A build script to bundle JS, CSS, and images for production, with
hashes and sourcemaps
...
What is the lifecycle methods order in mounting?
The lifecycle methods are called in the following order when an instance
of a component is being created and inserted into the DOM
...
2
...
4
...
45
What are the lifecycle methods going to be deprecated in React
v16?
The following lifecycle methods going to be unsafe coding practices and
will be more problematic with async rendering
...
componentWillMount()
2
...
componentWillUpdate()
Starting with React v16
...
� Back to Top
76
...
It can
return an object to update state, or null to indicate that the new props
do not require any state updates
...
Component {
static getDerivedStateFromProps(props, state) {
//
...
� Back to Top
77
...
The return value from this method will be passed
as the third parameter to componentDidUpdate()
...
Component {
getSnapshotBeforeUpdate(prevProps, prevState) {
//
...
� Back to Top
78
...
� Back to Top
79
...
Using displayName for naming component:
export default React
...
})
The recommended approach:
export default class TodoApp extends React
...
}
also
const TodoApp = () => {
//
...
What is the recommended ordering of methods in component
class?
Recommended ordering of methods from mounting to render stage:
1
...
constructor()
47
3
...
5
...
7
...
9
...
11
...
getter methods for render like getSelectReason() or getFooterContent()
13
...
render()
� Back to Top
81
...
We need to use object to map prop values to components
...
/HomePage’ import AboutPage
from ‘
...
/ServicesPage’ import ContactPage from ‘
...
page] || ContactPage
return
// The keys of the PAGES object can be used in the prop types to catch
dev-time errors
...
propTypes = { page: PropTypes
...
keys(PAGES))
...
Why we need to pass a function to setState()?
The reason behind for this is that setState() is an asynchronous operation
...
That means you
48
should not rely on the current state when calling setState() since you
can’t be sure what that state will be
...
By doing this you
can avoid issues with the user getting the old state value on access due to
the asynchronous nature of setState()
...
After three consecutive increment
operations, the value is going to be incremented only by one
...
state
...
setState({ count: this
...
count + 1 })
this
...
state
...
setState({ count: this
...
count + 1 })
// this
...
count === 1, not 3
If we pass a function to setState(), the count gets incremented correctly
...
setState((prevState, props) => ({
count: prevState
...
increment
}))
// this
...
count === 3 as expected
(OR)
Why function is preferred over object for setState()?
React may batch multiple setState() calls into a single update for performance
...
props and this
...
This counter example will fail to update as expected:
// Wrong
this
...
state
...
props
...
That function will receive the previous state as the first argument,
and the props at the time the update is applied as the second argument
...
setState((prevState, props) => ({
counter: prevState
...
increment
}))
� Back to Top
83
...
StrictMode is a useful component for highlighting potential problems in an application
...
It activates additional checks and warnings for its descendants
...
“‘jsx harmony import React from ‘react’
function ExampleApplication() { return (
StrictMode>
) } “‘
In the example above, the strict mode checks apply to
and
...
What are React Mixins?
Mixins are a way to totally separate components to have a common functionality
...
One of the most commonly used mixins is PureRenderMixin
...
createClass({
mixins: [PureRenderMixin],
//
...
50
Why is isMounted() an anti-pattern and what is the proper solution?
The primary use case for isMounted() is to avoid calling setState() after
a component has been unmounted, because it will emit a warning
...
isMounted()) {
this
...
})
}
Checking isMounted() before calling setState() does eliminate the warning, but it also defeats the purpose of the warning
...
An optimal solution would be to find places where setState() might be
called after a component has unmounted, and fix them
...
Ideally, any callbacks should be canceled in componentWillUnmount(), prior to unmounting
...
What are the Pointer Events supported in React?
Pointer Events provide a unified way of handling all input events
...
We need to remember that
these events will only work in browsers that support the Pointer Events
specification
...
2
...
4
...
6
...
8
...
10
...
51
Why should component names start with capital letter?
If you are rendering your component using JSX, the name of that
component has to begin with a capital letter otherwise React will throw
an error as an unrecognized tag
...
jsx harmony
class SomeComponent extends Component { // Code goes here }
You can define component class which name starts with lowercase letter,
but when it’s imported it should have capital letter
...
/MyComponent'
What are the exceptions on React component naming? The component names should start with an uppercase letter but there are few exceptions to this convention
...
For example, the
below tag can be compiled to a valid component,
jsx harmony
render() {
return (
// `React
...
component)`
Are custom DOM attributes supported in React v16?
Yes
...
If you
wrote JSX with an attribute that React doesn’t recognize, React would
just skip it
...
� Back to Top
89
...
createClass()
...
This way it can conditionally render component
...
loggedIn) {
return null;
}
return (
welcome, {props
...
Component {
constructor(props) {
super(props);
this
...
state
...
state
...
� Back to Top
267
...
1
...
The items in the list have no ids
3
...
� Back to Top
268
...
i
...
For example, the below Book component uses two arrays with different
arrays,
function Book(props) {
const index = (
-
...
title}
{props
...
map((page) =>
)}
);
const content = props
...
map((page) =>
...
title}
title}
{page
...
pageNumber}
);
return (
{index}
{content}
);
}
� Back to Top
269
...
In detail, You can categorize them as follows,
1
...
Validation and error messages
3
...
� Back to Top
270
...
The form state is inherently short-term and local, so tracking it in
Redux (or any kind of Flux library) is unnecessary
...
Redux-Form calls your entire top-level Redux reducer multiple times
ON EVERY SINGLE KEYSTROKE
...
3
...
5 kB minified gzipped whereas Formik is 12
...
Why are you not required to use inheritance?
In React, it is recommended to use composition over inheritance to reuse
code between components
...
Whereas, If you want to reuse non-UI functionality between components, it is suggested to extract it into a separate JavaScript
module
...
� Back to Top
272
...
Even though
many developers won’t use this combination, it may require especially
if you are using third-party UI components that are written using Web
Components
...
/App
...
129
What is dynamic import?
You can achieve code-splitting in your app using dynamic import
...
Normal Import
import { add } from '
...
log(add(10, 20));
2
...
/math")
...
log(math
...
What are loadable components?
If you want to do code-splitting in a server rendered app, it is recommend
to use Loadable Components because React
...
Loadable lets you render a dynamic
import as a regular component
...
/OtherComponent'))
function MyComponent() {
return (
)
}
Now OtherComponent will be loaded in a separated bundle
� Back to Top
275
...
This can be
done using Suspense component
...
lazy(() => import('
...
);
}
As mentioned in the above code, Suspense is wrapped above the lazy
component
...
What is route based code splitting?
One of the best place to do code splitting is with routes
...
Due to this, the user experience
won’t be disturbed
...
lazy,
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import React, { Suspense, lazy } from 'react';
const Home = lazy(() => import('
...
/routes/About'));
const App = () => (
...
� Back to Top
277
...
For example, in the code below lets manually thread through a “theme”
prop in order to style the Button component
...
createContext('luna');
// Create App component where it uses provider to pass theme value in the tree
class App extends React
...
Provider value="nova">
...
Component {
static contextType = ThemeContext;
render() {
return